package cn.smbms.service.impl;

import cn.smbms.dao.UserMapper;
import cn.smbms.pojo.User;
import cn.smbms.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> list(Integer pageNo, Integer pageSize, String username, Integer userRole) {
        //起始位置 =(当前页码-1)*页面大小
        Integer beginIndex = (pageNo-1)*pageSize;
        return userMapper.list(beginIndex,pageSize,username,userRole);
    }

    @Override
    public int count(String username, Integer userRole) {
        return userMapper.count(username,userRole);
    }


    @Override
    public boolean add(User user) {
        return userMapper.add(user)>0;
    }

    @Override
    public User login(String userCode, String userPassword) {

        //先按userCode去查用户
        User user = userMapper.findUserByUserCode(userCode);

        //如果user为null，则直接返回null
        if (null == user){
            return null;
        }

        //再比较userPassword是否一致
        if (user.getUserPassword().equals(userPassword)){
            return user;
        }

        return null;
    }

    @Override
    public User ucexist(String userCode) {
        return userMapper.findUserByUserCode(userCode);
    }

    @Override
    public User findUserById(Integer id) {
        return userMapper.findUserById(id);
    }

    @Override
    public boolean update(User user) {
        return userMapper.update(user)>0;
    }
}
